package com.cuit.xsc.weishare.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *@author coolszy
 *@date 2011-6-13
 *@blog http://blog.csdn.net/coolszy
 */

/**
 * 辅助类
 */
public class TextUtil
{
	public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str)
	{
		if (null==str||str.equals(""))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 分割返回结果
	 * @param response 返回字符串
	 * @return
	 */
	public static Map<String, String> splitResponse(String response)
	{
		if (response == null)
		{
			return null;
		}
		//保存返回结果
		Map<String, String> map = new HashMap<String, String>();
		//判断是否为空
		if (!TextUtil.isEmpty(response))
		{
			//已“&”进行分割
			String[] array = response.split("&");
			if (array.length == 3) 
			{
				String tokenStr = array[0]; //oauth_token=xxxxx
				String secretStr = array[1];//oauth_token_secret=xxxxxxx
				String paramter = array[2]; //user_id or username
				String[] token = tokenStr.split("=");
				if (token.length == 2) 
				{
					map.put("oauth_token", token[1]);
				}
				String[] secret = secretStr.split("=");
				if (secret.length == 2) 
				{
					map.put("oauth_token_secret", secret[1]);
				}
				String[] pars = paramter.split("=");
				if (pars.length == 2)
				{
					map.put("user", pars[1]);
				}
			}
		}
		return map;
	}
	
	public static void witeFile(String fileName, String content)
	{
		String path = "/sdcard/"+fileName;
		try
		{
			FileOutputStream fos = new FileOutputStream(new File(path));
			fos.write(content.getBytes());
			fos.close();
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static String  formatDate(Date date)
	{
		String result = null;
		if (date == null)
		{
			return null;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		result = sdf.format(date);
		return result;
		
	}
	
	
	public static String formatDate(long time)
	{
	    
		if ((time+"").length() == 10)
		{
			time = time*1000+8*60*60*1000;
		}
		Date date = new Date(time);
		return formatDate(date);
	}
	
	
	
}
